'use strict';

const Controller = require('egg').Controller;

class DynamicController extends Controller {

    async getMyDynamic() {

        const {
            ctx,
        } = this;

        var userId = ctx.userInfo.id

        const data = await ctx.model.Dynamic.findAll({
            'order': [
                ['post_time', 'DESC']
            ],
            where: {
                post_uid: userId
            },
            include: [{
                model: ctx.model.Info,
                as: 'publisher'
            }, {
                model: ctx.model.Dynamic,
                as: 'transmit',
                include: [{
                    model: ctx.model.Info,
                    as: 'publisher'
                }, {
                    model: ctx.model.Dynamic,
                    as: 'transmit'
                }],
            }],
            limit: 8
        })

        data.forEach(element => {
            switch (element.state) {
                case 0: {
                    //0:：文字图片帖，
                    element.img_src = element.img_src.split('@@');
                    break;
                }
                case 2: {
                    // //2：转发贴
                    let e = element.transmit;
                    switch (e.state) {
                        case 0: {
                            //0:：文字图片帖，
                            e.img_src = e.img_src.split('@@');
                        }
                    }
                    break;
                }
            }
        });

        ctx.response.body = {
            code: 200,
            msg: 'The query is successful !',
            data
        }
    }

    async postDynamic() {
        const {
            ctx,
        } = this;

        var userId = ctx.userInfo.id

        const {
            content,
            state,
            img_src = '',
            video_src = '',
            dynamic_id = null,
        } = ctx.request.body;

        await ctx.model.Dynamic.create({
            content,
            state,
            img_src,
            video_src,
            dynamic_id,
            post_uid:userId
        })

        ctx.response.body = {
            code: 200,
            msg: 'The query is successful !',
            data: userId
        }
    }
}

module.exports = DynamicController;